package com.apes.scm.advanceOrder.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.contract.model.Contract;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;

/**
 * 功能：预收（付）款单
 *
 * @author HuJianLin
 * @create 2018-11-30 17:36
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = AdvanceOrder.CONST_TABLE_NAME)
public class AdvanceOrder extends Model {
    public static final String CONST_TABLE_NAME = "acc_advance_payment";

    @Id
    @Column(name = "id")
    @Comment("预收款单号")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "申请单号", prefix = "2")
    private String id;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    @Comment("单据类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "advance_type", referencedColumnName = "id", nullable = false)
    private Enumeration advanceType; //advanceType ->（advReceipt：预收款；advPayment：预付款）

    @Comment("合同号")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", referencedColumnName = "id")
    private Contract contract;

    @Comment("合作方式")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id", nullable = false)
    private Cooperation cooperation;

    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("合作伙伴")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    private PartyRole partner;

    @Comment("备注")
    @Column(name = "remark", length = 120)
    private String remark;

    @Comment("单据状态")
    @States(name = "单据状态", values = {
            @State(id = "sent", name = "制单"),
            @State(id = "oa", name = "申请中"),
            @State(id = "done", name = "完成"),
            @State(id = "approve", name = "审核"),
            @State(id = "delete", name = "删除")})
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Comment("金额")
    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amount = 0; //金额

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID; //审核

    @Comment("审批日期")
    @Column(name = "date_approve")
    private Date dateApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUID; //删除

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate; //删除日期

    @Column(name = "bank", length = 200)
    @Comment("开户行")
    private String bank;

    @Column(name = "bank_account", length = 32)
    @Comment("银行帐号")
    private String bankAccount;

    @Column(name = "account", length = 32)
    @Comment("收款帐号")
    private String account;

    @Comment("银行账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bank_account_id", referencedColumnName = "id", nullable = false)
    private BankAccount bankAccountId;

    @Comment("支行")
    @Column(name = "subbranch", length = 200)
    private String subbranch;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "payment_date")
    @Comment("付款日期")
    private Date paymentDate;

    @Comment("付款回执")
    @Column(name = "file_address", length = 4000)
    private String fileAddres;

}
